﻿using System;
using System.Web;
using System.Web.UI;
using System.Configuration;
using SimplePMT.Data;
using SimplePMT.Control;
using System.Text;
using System.Collections.Generic;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace SimplePMT
{
    public partial class FileView : Components.BasePage
    {
        
        public string fileView = string.Empty;
        public string taskId = string.Empty;
        public int parentContainerId = 0;
        public string memberId = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidateSession();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(FileView), this.Page);
            if (Request.Form.Count.Equals(0))
            {
                Control.FileControl view = this.LoadControl("./Control/FileControl.ascx") as Control.FileControl;
                view.edit = true;
                view.attachmentid = Convert.ToInt32(Request.QueryString["id"]);
                view.containerId = Convert.ToInt32(Request.QueryString["cid"]);
                parentContainerId = TaskContainer.Find(view.containerId).ParentContainerID;
                taskId = view.attachmentid.ToString();
                fileView = view.ToString();
                memberId = Session["MemberID"].ToString();
            }
            else
            {
                string savePath = "C:\\SimplePMTFileStore";
                int attachmentId = 0;

                if (!Request.Form["AttachmentID"].Equals(null)) {
                    attachmentId = Convert.ToInt32(Request.Form["AttachmentID"]);
                }

                Attachment attachment = Attachment.Find(attachmentId) ?? new Attachment();
                bool isNew = false;
                if (attachment.ID > 0)
                {
                    attachment.UpdateDateTime = DateTime.Now;
                }
                else
                {
                    isNew = true;
                    attachment.Path = "";
                    attachment.DateTime = DateTime.Now;
                    attachment.UpdateDateTime = DateTime.Now;
                }
                attachment.Description = Request.Form["Description"];
                attachment.MemberID = Convert.ToInt32(Session["MemberID"]);
                
                attachment.Title = Request.Form["Title"].Replace("&quot;", "\"");
                attachment.Save();

                if (isNew)
                {
                    TaskContainerAttachment tca = new TaskContainerAttachment();
                    tca.Attachment = attachment;
                    tca.TaskContainer = TaskContainer.Find(Convert.ToInt32(Request.Form["TaskContainerID"]));
                    tca.Save();
                }
                if (Request.Files.Count > 0)
                {
                    if (Request.Files["AttachmentPath"].FileName.Trim().Length > 0)
                    {
                        string path = savePath + "\\" + attachment.ID + "_" + GetFileName(Request.Files["AttachmentPath"].FileName);
                        Request.Files["AttachmentPath"].SaveAs(path);
                        attachment.Path = attachment.ID + "_" + GetFileName(Request.Files["AttachmentPath"].FileName);
                        attachment.Save();
                    }
                }

                Response.Write("<script>top.win.close();</script>");
            }
        }

        [AjaxPro.AjaxMethod()]
        public bool AssociateTaskAttachment(int taskId, int attachmentId, bool associate)
        {
            TaskAttachment[] taa = TaskAttachment.FindAll(Expression.Eq("TaskID", taskId), Expression.Eq("AttachmentID", attachmentId));
            if (taa.Length.Equals(0) && associate)
            {
                TaskAttachment ta = new TaskAttachment();
                ta.TaskID = taskId;
                ta.AttachmentID = attachmentId;
                ta.Save();
            }
            else if (taa.Length > 0 && !associate)
            {
                taa[0].Delete();
            }
            return true;
        }

        [AjaxPro.AjaxMethod()]
        public bool DeleteFile(int attachmentId)
        {
            Attachment attachment = Attachment.Find(attachmentId);
            TaskContainerAttachment[] itms = TaskContainerAttachment.FindAllByProperty("Attachment", attachment);
            foreach (TaskContainerAttachment itm in itms)
            {
                itm.Delete();
            }
            
            attachment.Delete();
            return true;
        }

        public string GetFileName(string path)
        {
            string[] tmp = path.Split("\\".ToCharArray());
            return tmp[tmp.Length - 1];
        }
        

        
    }
}
